﻿var last_selected_delete_index;
var last_selected_action_index;

$(document).ready(function () {

    $("#delete").click(function () {
        var row_to_delete = document.getElementById("table_row" + last_selected_delete_index);
        row_to_delete.parentNode.removeChild(row_to_delete);
        $.ajax({
            type: 'POST',
            url: '/Survey/Delete/',
            data: { id: last_selected_delete_index },
        });
    });

    $("#actionConfirm").click(function () {
        actionToNextState(last_selected_action_index);
    });

});

function init() {

    //table 0
    var staged_table_section = document.getElementById("under_work_table");
    //table 1
    var active_table_section = document.getElementById("active_table");
    //table 2
    var inactive_table_section = document.getElementById("inactive_table");
    //table 3
    var view_table_section = document.getElementById("view_table");
    
    staged_table_section.appendChild(createTable(0));
    active_table_section.appendChild(createTable(1));
    inactive_table_section.appendChild(createTable(2));
    view_table_section.appendChild(createTable(3));
    
}

function addTableEntry(tableID, title, owner, date, index, view, currentUser) {
    if (tableID != 3 && currentUser == owner)
    {
        var table = document.getElementById("table" + tableID);
        table.appendChild(createRow(tableID, index, title, date, owner));
    }
    if (view>0)
    {
        var table1 = document.getElementById("table" + 3);
        table1.appendChild(createRow(3, index, title, date, owner));
    }
}

function createTable(tableID) {
    var tablediv = document.createElement("div");
    tablediv.setAttribute("class", "well");
    tablediv.setAttribute("style", "background-color:white;");
    var table = document.createElement("table");
    table.setAttribute("class", "table table-condensed table-hover");
    table.appendChild(createHeaderRow());
    table.appendChild(createTableBody(tableID));
    
    tablediv.appendChild(table);
    return tablediv;
}

function createHeaderRow() {
    var headerRow = document.createElement("thead");
    var row = document.createElement("tr");
    var nameColHeader = document.createElement("th");
    nameColHeader.appendChild(document.createTextNode("Survey Title"));
    var ownerColHeader = document.createElement("th");
    ownerColHeader.appendChild(document.createTextNode("Owner"));
    var dateColHeader = document.createElement("th");
    dateColHeader.appendChild(document.createTextNode("Creation Date"));
    var buttonColHeader = document.createElement("th");
    buttonColHeader.appendChild(document.createTextNode("Actions"));
    row.appendChild(nameColHeader);
    row.appendChild(ownerColHeader);
    row.appendChild(dateColHeader);
    row.appendChild(buttonColHeader);
    headerRow.appendChild(row);
    return headerRow;
}

function createTableBody(tableID) {
    var bodyTag = document.createElement("tbody");
    bodyTag.setAttribute("id", "table" + tableID);
    return bodyTag;
}

function createRow(tableID, identifier, title, date, owner) {

    /*
    *
    * Row and Column Declarations
    *
    */
    var row = document.createElement("tr");
    row.setAttribute("id", "table_row" + identifier);
    var nameCol = document.createElement("td");
    nameCol.appendChild(document.createTextNode(title));
    var ownerCol = document.createElement("td");
    ownerCol.appendChild(document.createTextNode(owner));
    var dateCol = document.createElement("td");
    dateCol.appendChild(document.createTextNode(date));
    var buttonCol = document.createElement("td");

    /* 
    *
    *Button Declarations
    *
    */
    
    //Delete Button - removes survey from database
    var deleteButton = document.createElement("a");
    deleteButton.setAttribute("href", "#deleteModal");
    deleteButton.setAttribute("role", "button");
    deleteButton.setAttribute("class", "btn btn-danger btn-small");
    deleteButton.setAttribute("data-toggle", "modal");
    deleteButton.setAttribute("id", identifier);
    deleteButton.setAttribute("title", "Delete Survey");
    $(deleteButton).click(function () {
        last_selected_delete_index = identifier;
    });
    var deleteIcon = document.createElement("i");
    deleteIcon.setAttribute("class", "icon-white icon-remove");
    deleteButton.appendChild(deleteIcon);


    //Action Button - transitions survey to next state
    var actionButton = document.createElement("a");
    actionButton.setAttribute("role", "button");
    actionButton.setAttribute("class", "btn btn-success btn-small");
    actionButton.setAttribute("data-toggle", "modal");
    actionButton.setAttribute("id", identifier);
    actionButton.setAttribute("title", "Action Survey to Next State");
    actionButton.setAttribute("href", "#actionModal");
    //even though this is just a function calling a function, it must remain.
    $(actionButton).click(function () { last_selected_action_index = identifier; });
    var actionIcon = document.createElement("i");
    actionIcon.setAttribute("class", "icon-white icon-chevron-down");
    actionButton.appendChild(actionIcon);

    //Details Button - displays response information
    var detailsButton = document.createElement("a");
    detailsButton.setAttribute("role", "button");
    detailsButton.setAttribute("class", "btn btn-info btn-small");
    detailsButton.setAttribute("data-toggle", "modal");
    detailsButton.setAttribute("id", identifier);
    detailsButton.setAttribute("title", "View Responses");
    //even though this is just a function calling a function, it must remain.
    $(detailsButton).click(function () { details(identifier) });
    var detailsIcon = document.createElement("i");
    detailsIcon.setAttribute("class", "icon-white icon-signal");
    detailsButton.appendChild(detailsIcon);

    //Edit Button - changes survey questions
    var editButton = document.createElement("a");
    editButton.setAttribute("role", "button");
    editButton.setAttribute("class", "btn btn-warning btn-small");
    editButton.setAttribute("data-toggle", "modal");
    editButton.setAttribute("id", identifier);
    editButton.setAttribute("title", "Edit Survey");
    //even though this is just a function calling a function, it must remain.
    $(editButton).click(function () { rowSelected(identifier) });
    var editIcon = document.createElement("i");
    editIcon.setAttribute("class", "icon-white icon-pencil");
    editButton.appendChild(editIcon);

    //Take Survey Button - provide a set of responses to survey
    var takeSurveyButton = document.createElement("a");
    takeSurveyButton.setAttribute("role", "button");
    takeSurveyButton.setAttribute("class", "btn btn-primary btn-small");
    takeSurveyButton.setAttribute("data-toggle", "modal");
    takeSurveyButton.setAttribute("id", identifier);
    takeSurveyButton.setAttribute("title", "Take Survey");
    //even though this is just a function calling a function, it must remain.
    $(takeSurveyButton).click(function () { takeSurvey(identifier) });
    var takeSurveyIcon = document.createElement("i");
    takeSurveyIcon.setAttribute("class", "icon-white icon-check");
    takeSurveyButton.appendChild(takeSurveyIcon);

    //Additon Button - Add Proctor to an Active Survey
    var additionButton = document.createElement("a");
    additionButton.setAttribute("role", "button");
    additionButton.setAttribute("class", "btn btn-primary btn-small");
    additionButton.setAttribute("data-toggle", "modal");
    additionButton.setAttribute("id", identifier);
    additionButton.setAttribute("title", "Add a Proctor");
    //even though this is just a function calling a function, it must remain.
    $(additionButton).click(function () { addProctor(identifier) });
    var additionIcon = document.createElement("i");
    additionIcon.setAttribute("class", "icon-white icon-plus");
    additionButton.appendChild(additionIcon);

    //Subtraction Button - Remove Proctor to an Active Survey
    var subtractButton = document.createElement("a");
    subtractButton.setAttribute("role", "button");
    subtractButton.setAttribute("class", "btn btn-inverse btn-small");
    subtractButton.setAttribute("data-toggle", "modal");
    subtractButton.setAttribute("id", identifier);
    subtractButton.setAttribute("title", "Add a Proctor");
    subtractButton.setAttribute("href", "#subtracModal");
    //even though this is just a function calling a function, it must remain.
    $(additionButton).click(function () { last_selected_action_index = identifier; });
    var subtractIcon = document.createElement("i");
    subtractIcon.setAttribute("class", "icon-white icon-minus");
    subtractButton.appendChild(subtractIcon);

    //Depending on which table the survey is in (based on state), different
    //buttons will be added to that survey.  however, each button behaves the same
    //regardless of state, so buttons can be re-used
    switch (tableID) {
        case 0: //In work surveys - Edit | Action | Delete
            buttonCol.appendChild(editButton);            
            buttonCol.appendChild(actionButton);
            buttonCol.appendChild(deleteButton);

            // We can't create a click event for the whole row because
            // it will also fire if we click the delete button so we
            // have to only fire for the columns we want.
            $(nameCol).live('click', function () {
                rowSelected(identifier);
            });
            $(ownerCol).live('click', function () {
                rowSelected(identifier);
            });
            $(dateCol).live('click', function () {
                rowSelected(identifier);
            });
            break;
        case 1: //Active Surveys - Take | Action            
            buttonCol.appendChild(takeSurveyButton);
            buttonCol.appendChild(actionButton);
            buttonCol.appendChild(additionButton);
            //buttonCol.appendChild(subtractButton);
            break;
        case 2://Inactive surveys - View responses | Delete            
            buttonCol.appendChild(detailsButton);
            buttonCol.appendChild(deleteButton);
            break;
        case 3://viewable - Delete            
            buttonCol.appendChild(takeSurveyButton);
            //buttonCol.appendChild(deleteButton);
            break;
        default:
        break;
    }
    
    row.appendChild(nameCol);
    row.appendChild(ownerCol);
    row.appendChild(dateCol);
    row.appendChild(buttonCol);
    return row;
}

//This function should fire the "Edit Survey" page and
//pull the survey information from the database.
function rowSelected(rowIdentifier) {
    window.location = "../Survey/Edit/" + rowIdentifier;

}

function details(rowIdentifier) {
    window.location = "../Survey/Details/" + rowIdentifier;

}

function actionToNextState(rowIdentifier) {
    window.location = "../Survey/Action/" + rowIdentifier;
}

function takeSurvey(rowIdentifier) {
    window.location = "../Survey/Take/" + rowIdentifier;
}

function addProctor(rowidentifier) {
    var retVal = prompt("Enter proctor name: ", "proctor name");
    alert("You have entered : " + retVal);
        $.ajax({
            type: 'POST',
            url: '/Survey/AddProctorToSurvey/',
            data: { id: rowidentifier, name: retVal },
        });
}